package org.example.lotterysystem.service.activitystatus.operater;

import org.example.lotterysystem.dao.dataobject.ActivityDO;
import org.example.lotterysystem.dao.dataobject.ActivityPrizeDO;
import org.example.lotterysystem.dao.mapper.ActivityMapper;
import org.example.lotterysystem.dao.mapper.ActivityPrizeMapper;
import org.example.lotterysystem.service.dto.ConvertActivityStatusDTO;
import org.example.lotterysystem.service.enmus.ActivityPrizeStatusEnum;
import org.example.lotterysystem.service.enmus.ActivityStatusEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class PrizeOperator extends AbstractActivityOperator{
    @Autowired
    private ActivityPrizeMapper activityPrizeMapper;

    @Override
    public Integer sequence() {
        return 1;
    }

    /**
     * 判断活动关联奖品是否需要转换
     * @param convertActivityStatusDTO
     * @return
     */
    @Override
    public Boolean needConvert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        //这里是判断活动id和目标奖品状态是否存在,奖品id是否存在，这些都是自己封装好的数据都是，id这一类都是从前端传过来的。
        Long activityId = convertActivityStatusDTO.getActivityId();
        ActivityPrizeStatusEnum targetPrizeStatus = convertActivityStatusDTO.getTargetPrizeStatus();
        Long prizeId = convertActivityStatusDTO.getPrizeId();
        if(activityId==null||targetPrizeStatus==null||prizeId==null){
            return false;
        }
        //查询活动关联奖品表，奖品是否存在
        ActivityPrizeDO activityPrizeDO = activityPrizeMapper.selectByAPId(activityId, prizeId);
        if(activityPrizeDO==null){
            return false;
        }
        //判断当前奖品与目标奖品状态是否一致
        if(activityPrizeDO.getStatus().equalsIgnoreCase(convertActivityStatusDTO.getTargetPrizeStatus().name())){
            //这里要明白，之前封装的数据时已完成，从数据库里面查询到和封装的数据一样也就意味着不需要更新。
            return false;
        }

        return true;
    }

    /**
     * 转换活动关联奖品，更新数据库
     * @param convertActivityStatusDTO
     * @return
     */
    @Override
    public Boolean convert(ConvertActivityStatusDTO convertActivityStatusDTO) {
        Long activityId = convertActivityStatusDTO.getActivityId();
        String status = convertActivityStatusDTO.getTargetPrizeStatus().name();
        Long prizeId = convertActivityStatusDTO.getPrizeId();
        try{
            activityPrizeMapper.updateStatus(activityId,prizeId,status);
            return true;
        }catch (Exception e){
            return false;
        }

    }
}
